package com.neuedu.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.neuedu.dao.IGoodDAO;
import com.neuedu.entity.Goods;
import com.neuedu.sql.QueryUpdate;
import com.neuedu.sql.ResultSetObject;

public class GoodDAOImp implements IGoodDAO {

	@Override
	public List<Goods> selectAll() {
		String sql="select * from t_goods";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql);
		return goodslist;
	}

	@Override
	public List<Goods> selectAll(Integer gflag, int offSet, int pageNum) {
		String sql="select * from t_goods where gflag=? limit ?,?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,gflag,offSet,pageNum);
		return goodslist;
	}

	@Override
	public List<Goods> selectAll(Integer gflag) {
		String sql="select * from t_goods where gflag=?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,gflag);
		return goodslist;
	}

	@Override
	public List<Goods> selectAll(int offSet, int pageNum) {
		
		String sql="select * from t_goods limit ?,?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,offSet,pageNum);
		return goodslist;
	}

	@Override
	public Goods selectOneByGnum(String gnum) {
		String sql="select * from t_goods where gnum=?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,gnum);
		if(goodslist==null) {
			return null;
		}
		return goodslist.get(0);
	}

	@Override
	public List<Goods> selectByType(Integer tid) {
		
		String sql="select * from t_goods where tid=?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,tid);
		return goodslist;
	}

	@Override
	public int selectCount(Integer gflag) {
		String sql="select count(*) from t_goods where gflag=?";
		List<Integer> count=QueryUpdate.query(new ResultSetObject<Integer>() {

			@Override
			public Integer getObject(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, sql, gflag);
		if(count==null) {
			return 0;
		}
		return count.get(0);
	}

	@Override
	public int selectCount() {
		String sql="select count(*) from t_goods";
		List<Integer> count=QueryUpdate.query(new ResultSetObject<Integer>() {

			@Override
			public Integer getObject(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, sql);
		if(count==null) {
			return 0;
		}
		return count.get(0);
	}

	@Override
	public List<Goods> selectByName(String gname, Integer gflag, int offSet, int pageNum) {
	
		String sql="select * from t_goods where gname like ? and gflag=? limit ?,?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,"%"+gname+"%",gflag,offSet,pageNum);
		return goodslist;
	}

	@Override
	public int selectCount(String gname, Integer gflag) {
		String sql="select count(*) from t_goods where gname like ? and gflag=?";
		List<Integer> count=QueryUpdate.query(new ResultSetObject<Integer>() {

			@Override
			public Integer getObject(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, sql,"%"+gname+"%", gflag);
		if(count==null) {
			return 0;
		}
		return count.get(0);
	}

	@Override
	public List<Goods> selectByTypeId(Integer tid, Integer gflag, int offSet, int pageNum) {
		String sql="select * from t_goods where tid=? and gflag=? limit ?,?";
		List<Goods> goodslist=QueryUpdate.query(new ResultSetObject<Goods>() {

			@Override
			public Goods getObject(ResultSet rs) throws SQLException {
				Goods goods=new Goods();
				goods.setGid(rs.getInt("gid"));
				goods.setGnum(rs.getString("gnum"));
				goods.setGname(rs.getString("gname"));
				goods.setTid(rs.getInt("tid"));
				goods.setGprice(rs.getBigDecimal("gprice"));
				goods.setGstock(rs.getInt("gstock"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setGmainimg(rs.getString("gmainimg"));
				goods.setGflag(rs.getInt("gflag"));
				goods.setCreatetime(rs.getTimestamp("createtime"));
				goods.setUpdatetime(rs.getTimestamp("updatetime"));
				return goods;
			}
		}, sql,tid,gflag,offSet,pageNum);
		return goodslist;
	}

	@Override
	public int selectCount(Integer tid, Integer gflag) {
		String sql="select count(*) from t_goods where tid=? and gflag=?";
		List<Integer> count=QueryUpdate.query(new ResultSetObject<Integer>() {

			@Override
			public Integer getObject(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, sql,tid, gflag);
		if(count==null) {
			return 0;
		}
		return count.get(0);
	}

}
